import { useAppDispatch, useAppSelector } from '@/store';
import { getReplyByIdAction, saveOriginAction } from '@/store/action/comment';
import { NavBar } from 'antd-mobile';
import { map } from 'lodash';
import { useEffect } from 'react';
import CommentFooter from '../CommentFooter';
import CommentItem from '../CommentItem';
import NoComment from '../NoComment';
import styles from './index.module.scss';

/*
  学习目标：组件卸载时清除回复的评论列表
*/

export default function CommentReply() {
  const dispatch = useAppDispatch();

  const { originComment, replyData } = useAppSelector((state) => state.comment);
  const { results } = replyData;

  useEffect(() => {
    dispatch(getReplyByIdAction(originComment.com_id));

    // 1.
    return () => {
      dispatch({ type: 'comment/saveReply', payload: {} });
    };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [dispatch]);
  return (
    <div className={styles.root}>
      <div className="reply-wrapper">
        <NavBar className="transparent-navbar" onBack={() => dispatch(saveOriginAction({} as any))}>
          <div>{originComment.reply_count}条回复</div>
        </NavBar>

        <div className="origin-comment">
          <CommentItem comment={originComment} type="origin" />
        </div>

        <div className="reply-list">
          <div className="reply-header">全部回复</div>

          {results?.length ? (
            map(results, (item) => {
              return <CommentItem comment={item} type="reply" />;
            })
          ) : (
            <NoComment />
          )}
        </div>

        <CommentFooter type="reply" />
      </div>
    </div>
  );
}
